package com.atguigu.tingshu.search.service.impl;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.model.album.BaseCategoryView;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumInfoDetailVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {
	@Autowired
	private UserInfoFeignClient userInfoFeignClient;
	@Autowired
	private AlbumInfoFeignClient albumInfoFeignClient;
	@Autowired
	private CategoryFeignClient categoryFeignClient;

	@Override
	public AlbumInfoDetailVo item(Long albumId) {
		AlbumInfoDetailVo albumInfoDetailVo = new AlbumInfoDetailVo();
		// 专辑详情基本数据
		CompletableFuture<AlbumInfo> cf1 = CompletableFuture.supplyAsync(() -> {
			AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfo(albumId).getData();
			albumInfoDetailVo.setAlbumInfo(albumInfo);
			return albumInfo;
		});
		// 专辑分类数据
		CompletableFuture<Void> cf2 = cf1.thenAcceptAsync(albumInfo -> {
			albumInfoDetailVo.setBaseCategoryView(categoryFeignClient.findBaseCategoryView(albumInfo.getCategory3Id()).getData());
		});
		// 专辑主播数据
		CompletableFuture<Void> cf3 = cf1.thenAcceptAsync(albumInfo -> {
			albumInfoDetailVo.setAnnouncer(userInfoFeignClient.findUserById(albumInfo.getUserId()).getData());
		});
		// 专辑统计数据
		CompletableFuture<Void> cf4 = CompletableFuture.runAsync(() -> {
			albumInfoDetailVo.setAlbumStatVo(albumInfoFeignClient.getAlbumStatByAlbumId(albumId).getData());
		});
		CompletableFuture.allOf(cf1,cf2,cf3,cf4).join();
		return albumInfoDetailVo;
	}
}
